using Godot;
using System;

public partial class Walk : AnimatedSprite2D 
{

	[Signal]
	public delegate void MySignalEventHandler(string message);
	private float MoveSpeed = 350.0f;
	private Sprite2D _digiaSprite;
	private Sprite2D _siblingSprite;
	private Sprite2D _playFaceSprite;
	private Sprite2D _fakerFaceSprite;
	private int direct=2;
	private bool input=true;
	private bool animate=true;
	private bool stopWalkL=false;
	private bool stopWalkR=false;
	private string[]  diaList={
		"你好，请问这是在哪里？","这里是你的记忆空间，你会出现在这里那么说明你丢失了很重要的记忆","那么你是谁","我是，是帮助你找回丢失的记忆","。。。。。。，感觉很可疑","哎呀，相信我啦，我这么可爱，怎么会说谎呢>V<",
		"。。。。。。,好吧，那我要怎么做呢","打败泰拉，它是将人类记忆作为食粮的恶魔","。。。。。。","哎呀，相信我啦","行吧","那我们出发吧"
	};
	private int diaIndex=0 ;
	private bool startEnter=false;
	private Vector2 _velocity = Vector2.Zero;
	
	public override void _PhysicsProcess(double delta)
	{
		_velocity = Vector2.Zero;
		if (Input.IsActionPressed("LEFT")&&input)
		{
			_velocity.X += 1.0f;
			_velocity = _velocity.Normalized() * MoveSpeed;
			if(!stopWalkL){
				_siblingSprite.Position += _velocity * (float)delta;
			}
			if(direct==2){
				direct=1;
				this.FlipH = !this.FlipH;
			}
			if(!this.IsPlaying()&&this.animate){
				this.Play("walk");
			}
		}
		if(Input.IsActionPressed("RIGHT")&&input){
			_velocity.X -= 1.0f;
			_velocity = _velocity.Normalized() * MoveSpeed;
			if(!stopWalkR){
				_siblingSprite.Position += _velocity * (float)delta;
			}
			if(direct==1){
				direct=2;
				this.FlipH = !this.FlipH;
			}
			if(!this.IsPlaying()&&this.animate){
				this.Play("walk");
			}
		}
		if(Input.IsActionPressed("NextDia")&&startEnter){
			if(diaList.Length==diaIndex){
				GetTree().ChangeSceneToFile("res://scene/stage2.tscn");
			}
			startEnter=false;
			if(diaIndex%2==0){
				this._playFaceSprite.Visible =true;
				this._fakerFaceSprite.Visible =false;
			}else{
				this._playFaceSprite.Visible =false;
				this._fakerFaceSprite.Visible =true;
			}
			EmitSignal(SignalName.MySignal, diaList[diaIndex++]);
			
		}
		
		
		if (Input.IsActionJustReleased("LEFT")||Input.IsActionJustReleased("RIGHT"))
		{
			if(this.IsPlaying()){
				 this.Stop();
			}
		   
		}
	}
	
	public void OnBodyEntered(Node body)
	{
		if(body.Name=="Area2D"){
			this.stopWalkL=true;
		}else{
			this.stopWalkR=true;
			this.animate=false;
			this.input=false;
			this._digiaSprite.Visible =true;
			this._playFaceSprite.Visible =true;
			EmitSignal(SignalName.MySignal, diaList[diaIndex++]);
			
		}
	}
	
	public void OnBodyExited(Node body)
	{
		if(body.Name=="Area2D"){
			this.stopWalkL=false;
		}else{
		}
	}
	
	public void nextDia()
	{
		startEnter=true;
	}
	
	public override void _Ready()
	{
		_playFaceSprite= GetNode<Sprite2D>("PlayFace");
		_fakerFaceSprite= GetNode<Sprite2D>("FakerFace");
		_siblingSprite = GetParent().GetNode<Sprite2D>("bg");
		_digiaSprite = GetNode<Sprite2D>("Digia");
		
	}
	
	
}
